package com.cn.dream_order_server.service.impl;

import com.cn.dream_order_server.async.WlcLogisticsManagementAsync;
import com.cn.dream_order_server.client.UserSiteServer;
import com.cn.dream_order_server.mapper.*;
import com.cn.dream_order_server.pojo.*;
import com.cn.dream_order_server.service.OrderOrderBackstageService;
import com.cn.dream_order_server.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderOrderBackstageServiceImpl implements OrderOrderBackstageService {

    @Autowired
    private WlcLogisticsManagementAsync wlcLogisticsManagementAsync;

    @Autowired
    private OrderOrderMapper orderOrderMapper;

    @Autowired
    private OrderOrdertypeMapper orderOrdertypeMapper;

    @Autowired
    private OrderOrderdetailsMapper orderOrderdetailsMapper;

    @Autowired
    private OrderOrdertypeManageMapper orderOrdertypeManageMapper;

    @Autowired
    private OrderPaytypeMapper orderPaytypeMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired
    private OrderLogisticstypeMapper orderLogisticstypeMapper;

    @Autowired
    private OrderPeymodeMapper orderPeymodeMapper;

    @Autowired
    private UserSiteServer userSiteServer;


    @Override
    public List<OrderOrder> getOrderOrderList(String orderId, String placeTime) {
            //创建全部数据
        OrderOrderExample example1 = new OrderOrderExample();
        example1.setOrderByClause("place_time desc");
        OrderOrderExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andOrderOther4NotEqualTo(6);
        if (orderId!=null&&!"".equals(orderId)){
            criteria1.andOrderIdLike("%"+orderId+"%");
        }
        if (placeTime!=null && !"".equals(placeTime)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String placeTime1 = placeTime+" 00:00:00";
            String placeTime2 = placeTime+" 24:00:00";
            Date date1 = null;
            Date date2 = null;
            try {
                date1 = format.parse(placeTime1);
                date2 = format.parse(placeTime2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria1.andPlaceTimeBetween(date1,date2);
        }
        List<OrderOrder> list = orderOrderMapper.selectByExample(example1);
        //遍历集合   将订单状态存入订单对象中
        for (OrderOrder orderOrder:list ) {
            orderOrder.setOrderOrdertype(orderOrdertypeMapper.selectByPrimaryKey(orderOrder.getOrdertypeId()));
            //设置数量
            OrderOrderdetailsExample example = new OrderOrderdetailsExample();
            OrderOrderdetailsExample.Criteria criteria = example.createCriteria();
            criteria.andOrderIdEqualTo(orderOrder.getOrderId());
            orderOrder.setShoppingNum((int) orderOrderdetailsMapper.countByExample(example));
            orderOrder.setOrderOrdertypeManage(orderOrdertypeManageMapper.selectByPrimaryKey(orderOrder.getOrderOther4()));
        }
        return list;
    }

    @Override
    public OrderOrder getOrderOrderByOrderId(String orderId) {


        OrderOrder orderOrder = orderOrderMapper.selectByPrimaryKey(orderId);
        if (orderOrder!= null){
            String s = userSiteServer.getUserSiteBySiteId(orderOrder.getSiteId());
            orderOrder.setUserSite(JsonUtils.stringToObj(s,UserSite.class));
            orderOrder.setOrderPaytype(orderPaytypeMapper.selectByPrimaryKey(orderOrder.getPaytypeId()));
            orderOrder.setOrderPeymode(orderPeymodeMapper.selectByPrimaryKey(orderOrder.getPayMode()));
            if(orderOrder.getOrderOther1()!=null && !"".equals(orderOrder.getOrderOther1())){
                OrderLogistics orderLogistics = orderLogisticsMapper.selectByPrimaryKey(orderOrder.getOrderOther1());
                orderLogistics.setOrderLogisticstype(orderLogisticstypeMapper.selectByPrimaryKey(orderLogistics.getLogisticstypeId()));
                orderOrder.setOrderLogistics(orderLogistics);
            }
        }
        return orderOrder;
    }



    @Override
    public Boolean updateOrderOrderByOrderId(OrderOrder orderOrder) {
        OrderOrder orderOrder1 = orderOrderMapper.selectByPrimaryKey(orderOrder.getOrderId());
        Integer i = null;
        if(orderOrder1!=null){
            i = orderOrderMapper.updateByPrimaryKeySelective(orderOrder);
        }
        if (i==1){
            wlcLogisticsManagementAsync.randomLogisticsManagement(orderOrder);
            return true;
        }
        return false;
    }
   /*@Override
   public Boolean updateOrderOrderByOrderId(OrderOrder orderOrder) {return false;}*/


    @Override
    public List<OrderOrder> getOrderOrderListByOrderOther4(String orderId, String placeTime, Integer orderOther4) {
        //创建全部数据
        OrderOrderExample example1 = new OrderOrderExample();
        example1.setOrderByClause("place_time desc");
        OrderOrderExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andOrderOther4EqualTo(orderOther4);
        if (orderId!=null&&!"".equals(orderId)){
            criteria1.andOrderIdLike("%"+orderId+"%");
        }
        if (placeTime!=null && !"".equals(placeTime)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String placeTime1 = placeTime+" 00:00:00";
            String placeTime2 = placeTime+" 24:00:00";
            Date date1 = null;
            Date date2 = null;
            try {
                date1 = format.parse(placeTime1);
                date2 = format.parse(placeTime2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria1.andPlaceTimeBetween(date1,date2);
        }
        List<OrderOrder> list = orderOrderMapper.selectByExample(example1);
        //遍历集合   将订单状态存入订单对象中
        for (OrderOrder orderOrder:list ) {
            orderOrder.setOrderOrdertype(orderOrdertypeMapper.selectByPrimaryKey(orderOrder.getOrdertypeId()));
            //设置数量
            OrderOrderdetailsExample example = new OrderOrderdetailsExample();
            OrderOrderdetailsExample.Criteria criteria = example.createCriteria();
            criteria.andOrderIdEqualTo(orderOrder.getOrderId());
            orderOrder.setShoppingNum((int) orderOrderdetailsMapper.countByExample(example));
            orderOrder.setOrderOrdertypeManage(orderOrdertypeManageMapper.selectByPrimaryKey(orderOrder.getOrderOther4()));
        }
        return list;
    }

    @Override
    public Integer deleteOrderOrderByOrderId(String orderId) {
        //根据id获取指定对象
        OrderOrder orderOrder = orderOrderMapper.selectByPrimaryKey(orderId);
        //判断对象是否为空
        if (orderOrder!=null){
            //设置orderOther4的id为6（已删除）
            orderOrder.setOrderOther4(6);
            //对数据进行修改
            return  orderOrderMapper.updateByPrimaryKeySelective(orderOrder);
        }


        return null;
    }

    @Override
    public Long countAllOrderOrderNum() {
        //创建条件
        OrderOrderExample example = new OrderOrderExample();
        OrderOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderOther4NotEqualTo(6);
        return orderOrderMapper.countByExample(example);
    }

    @Override
    public Integer baseUpdateOrderOrderByOrderId(OrderOrder orderOrder) {


        return  orderOrderMapper.updateByPrimaryKeySelective(orderOrder);
    }


}
